
from flask import request
from flask_login import current_user
from flask_restful import Resource, marshal_with,reqparse  # type: ignore
from controllers.console import api
from controllers.errors.error import HttpAccountNotFound
from controllers.errors.robot_user_errors import HTTPRobotUserParameterError
from core.roles.role_check import check_perms
from libs.helper import extract_remote_ip
from libs.login import login_required
# from fields.robot import base_timbre_fields
from werkzeug.exceptions import InternalServerError
from models.enums import AccountStatus
from services.errors.account import AccountNotFoundError, RobotUserParameterError
from services.robot.robot_user_service import RobotUserService
from fields.robot.robots_fields_def import robot_fields
from fields.robot.robot_user_fields_def import robot_user_fields,robot_user_list_fields,base_rob_extra_info_fields
class UserManageApi(Resource):

    @login_required
    @check_perms(perms=['users:list'])
    @marshal_with(robot_user_list_fields)
    def get(self):
        argparser = reqparse.RequestParser()
        argparser.add_argument("phone",type=str,required=False,location='args')
        argparser.add_argument("name",type=str,required=False,location='args')
        argparser.add_argument("postal_code",type=str,required=False,location='args')
        argparser.add_argument("status",type=str,required=False,location='args')
        argparser.add_argument("page",type=int,required=False,default=-1,location='args')
        argparser.add_argument("page_size",type=int,required=False,default=-1,location='args')
        args = argparser.parse_args()
        phone = args.get("phone")
        name = args.get("name")
        postal_code = args.get("postal_code")
        status = args.get("status")
        if status and status not in AccountStatus.choices():
            return {"message": "Invalid status"}, 400
        page = args.get("page",-1)
        page_size = args.get("page_size",-1)# "00000000-0000-0000-0000-000000000000"
        # return RobotUserService.get_user_list(current_user.current_tenant_id,name,phone,postal_code,status,page,page_size)
        return RobotUserService.get_user_list("00000000-0000-0000-0000-000000000000",name,phone,postal_code,status,page,page_size)
        
    @login_required
    @check_perms(perms=['users:new'])
    @marshal_with(robot_user_fields)
    def put(self):
        argparser = reqparse.RequestParser()
        argparser.add_argument("id",type=str,required=True,location='json')
        argparser.add_argument("phone",type=str,required=False,location='json')
        argparser.add_argument("name",type=str,required=False,location='json')
        argparser.add_argument("gender",type=int,required=False,location='json')
        # argparser.add_argument("postal_code",type=str,required=False,location='json')
        argparser.add_argument("status",type=str,required=False,location='json')
        argparser.add_argument("interface_language",type=str,required=False,location='json')
        argparser.add_argument("timezone",type=str,required=False,location='json')
        args = argparser.parse_args()
        try:
            return RobotUserService.update_user(current_user,args,extract_remote_ip(request))
        except RobotUserParameterError as e:
            raise HTTPRobotUserParameterError(e.description)
        except AccountNotFoundError:
            raise HttpAccountNotFound()
        except Exception as e:
            raise InternalServerError(str(e))

class RobotUserExtraInfoApi(Resource):
    @login_required
    @check_perms(perms=['users:update'])
    @marshal_with(base_rob_extra_info_fields)
    def put(self):
        argparser = reqparse.RequestParser()
        argparser.add_argument("account_id",type=str,required=True,location='json')
        argparser.add_argument("robot_id",type=str,required=False,location='json')
        argparser.add_argument("user_name",type=str,required=False,location='json')
        argparser.add_argument("postal_code",type=int,required=False,location='json')
        argparser.add_argument("extra_info",type=str,required=False,location='json')
        args = argparser.parse_args()
        try:
            return RobotUserService.update_user_extra_info(current_user,args,extract_remote_ip(request))
        except RobotUserParameterError as e:
            raise HTTPRobotUserParameterError(e.description)
        except AccountNotFoundError:
            raise HttpAccountNotFound()
        except Exception as e:
            raise InternalServerError(str(e))
class RobotUserRobotsApi(Resource):
    @login_required
    @check_perms(perms=['users:list'])
    @marshal_with(robot_fields)
    def get(self,account_id:str):
        return RobotUserService.get_user_robots(current_user.current_tenant_id,account_id)
    # return RobotUserService.get_user_robots(current_user.current_tenant_id,account_id)
    
api.add_resource(RobotUserRobotsApi, "/robots/users/robots/<account_id>")
api.add_resource(RobotUserExtraInfoApi, "/robots/users/extra_info")
api.add_resource(UserManageApi, "/robots/users")